/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clusterAnalysis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import units.CellType;
import units.DifferentialExpression;
import units.DifferentialExpressionLibrary;
import units.DiseaseStage;
import units.GeneLibrary;
import units.MonkeyType;
import utils.FileUtils;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class OutputMaxExpStats {

    private DifferentialExpressionLibrary del = new DifferentialExpressionLibrary();
    private GeneLibrary geneLibrary = new GeneLibrary();
    private File outputFolder = new File("/Users/jamie/Dropbox/monkeys/clust/stats/maxExp");

    public OutputMaxExpStats() {
        del.populateForAllMonkeyTypes(geneLibrary);
        try {
            makeOutput_maxExp(new File(outputFolder, "maxExp_M.txt"), MonkeyType.M);
            makeOutput_maxExp(new File(outputFolder, "maxExp_ML.txt"), MonkeyType.M, CellType.L);
            makeOutput_maxExp(new File(outputFolder, "maxExp_MP.txt"), MonkeyType.M, CellType.P);
            makeOutput_maxExp(new File(outputFolder, "maxExp_A.txt"), MonkeyType.A);
            makeOutput_maxExp(new File(outputFolder, "maxExp_AL.txt"), MonkeyType.A, CellType.L);
            makeOutput_maxExp(new File(outputFolder, "maxExp_AP.txt"), MonkeyType.A, CellType.P);
            makeOutput_maxExpTimePoint(new File(outputFolder, "maxExpTimePoint_M.txt"), MonkeyType.M);
            makeOutput_maxExpTimePoint(new File(outputFolder, "maxExpTimePoint_ML.txt"), MonkeyType.M, CellType.L);
            makeOutput_maxExpTimePoint(new File(outputFolder, "maxExpTimePoint_MP.txt"), MonkeyType.M, CellType.P);
            makeOutput_maxExpTimePoint(new File(outputFolder, "maxExpTimePoint_A.txt"), MonkeyType.A);
            makeOutput_maxExpTimePoint(new File(outputFolder, "maxExpTimePoint_AL.txt"), MonkeyType.A, CellType.L);
            makeOutput_maxExpTimePoint(new File(outputFolder, "maxExpTimePoint_AP.txt"), MonkeyType.A, CellType.P);
        } catch (IOException ex) {
            Logger.getLogger(OutputMaxExpStats.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void makeOutput_maxExpTimePoint(File file, MonkeyType monkeyType) throws IOException {

        TreeMap<Integer, List<DifferentialExpression>> deGenes = del.getDifferentialExpressedGenes(monkeyType, DiseaseStage.POST, 0.1);
        TreeMap<Integer, Double> maxExp = new TreeMap<Integer, Double>();
        TreeMap<Integer, Integer> timePoints = new TreeMap<Integer, Integer>();
        List<Integer> isgs = getISGs(monkeyType);
        for(int id : deGenes.keySet()){
            if(isgs.contains(id)){
                maxExp.put(id, -Double.MAX_VALUE);
                for(DifferentialExpression de : deGenes.get(id)){
                    if(de.getLogExp()>maxExp.get(id)){
                        maxExp.put(id, de.getLogExp());
                        timePoints.put(id, de.getTimePoint());
                    }
                }
            }
        }
        BufferedWriter writer = FileUtils.getBufferedWriter(file);
        for(int tp : timePoints.values()){
            writer.append(tp+"\n");
        }
        writer.close();
    }

    private void makeOutput_maxExpTimePoint(File file, MonkeyType monkeyType, CellType ct) throws IOException {

        TreeMap<Integer, List<DifferentialExpression>> deGenes = del.getDifferentialExpressedGenes(monkeyType, ct, DiseaseStage.POST, 0.1);
        TreeMap<Integer, Double> maxExp = new TreeMap<Integer, Double>();
        TreeMap<Integer, Integer> timePoints = new TreeMap<Integer, Integer>();
        List<Integer> isgs = getISGs(monkeyType, ct);
        for(int id : deGenes.keySet()){
            if(isgs.contains(id)){
                maxExp.put(id, -Double.MAX_VALUE);
                for(DifferentialExpression de : deGenes.get(id)){
                    if(de.getLogExp()>maxExp.get(id)){
                        maxExp.put(id, de.getLogExp());
                        timePoints.put(id, de.getTimePoint());
                    }
                }
            }
        }
        BufferedWriter writer = FileUtils.getBufferedWriter(file);
        for(int tp : timePoints.values()){
            writer.append(tp+"\n");
        }
        writer.close();
    }

    private void makeOutput_maxExp(File file, MonkeyType monkeyType) throws IOException {
        
        TreeMap<Integer, List<DifferentialExpression>> deGenes = del.getDifferentialExpressedGenes(monkeyType, DiseaseStage.POST, 0.1);
        TreeMap<Integer, Double> maxExp = new TreeMap<Integer, Double>();
        List<Integer> isgs = getISGs(monkeyType);
        for(int id : deGenes.keySet()){
            if(isgs.contains(id)){
                maxExp.put(id, -Double.MAX_VALUE);
                for(DifferentialExpression de : deGenes.get(id)){
                    if(de.getLogExp()>maxExp.get(id)){
                        maxExp.put(id, de.getLogExp());
                    }
                }
            }
        }
        BufferedWriter writer = FileUtils.getBufferedWriter(file);
        for(double max : maxExp.values()){
            writer.append(max+"\n");
        }
        writer.close();
    }

    private void makeOutput_maxExp(File file, MonkeyType monkeyType, CellType ct) throws IOException {

        TreeMap<Integer, List<DifferentialExpression>> deGenes = del.getDifferentialExpressedGenes(monkeyType, ct, DiseaseStage.POST, 0.1);
        TreeMap<Integer, Double> maxExp = new TreeMap<Integer, Double>();
        List<Integer> isgs = getISGs(monkeyType, ct);
        for(int id : deGenes.keySet()){
            if(isgs.contains(id)){
                maxExp.put(id, -Double.MAX_VALUE);
                for(DifferentialExpression de : deGenes.get(id)){
                    if(de.getLogExp()>maxExp.get(id)){
                        maxExp.put(id, de.getLogExp());
                    }
                }
            }
        }
        BufferedWriter writer = FileUtils.getBufferedWriter(file);
        for(double max : maxExp.values()){
            writer.append(max+"\n");
        }
        writer.close();
    }

    private List<Integer> getISGs(MonkeyType mt, CellType ct) {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql;
        sql = "select distinct t2.probeId from hcv.isgs t1, expression t2, sample t3 where t2.gId=t1.gId and t3.id=t2.sampleId and t3.monkeyType='" + mt.name() + "' and t3.cellType='" + ct.name() + "'";
        ResultSet rs = handle.runSQLquery(sql);
        List<Integer> ids = new ArrayList<Integer>();
        try {
            while (rs.next()) {
                ids.add(rs.getInt("probeId"));
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        handle.closeConnection();
        return ids;
    }

    private List<Integer> getISGs(MonkeyType mt) {
        Handle handle = Handle.getConnectedHandle("monkeys");
        String sql;
        sql = "select distinct t2.probeId from hcv.isgs t1, expression t2, sample t3 where t2.gId=t1.gId and t3.id=t2.sampleId and t3.monkeyType='" + mt.name() + "'";
        ResultSet rs = handle.runSQLquery(sql);
        List<Integer> ids = new ArrayList<Integer>();
        try {
            while (rs.next()) {
                ids.add(rs.getInt("probeId"));
            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        handle.closeConnection();
        return ids;
    }



}
